package com.quark.service.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quark.constant.CacheConstants;
import com.quark.constant.DictTypeEnum;
import com.quark.mapper.system.SysDictItemMapper;
import com.quark.mapper.system.SysDictMapper;
import com.quark.model.system.dict.SysDict;
import com.quark.model.system.dict.SysDictItem;
import com.quark.result.RequestResult;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 张高昌
 * @version 版本
 * @date 2022/01/09 14:23
 * @description: 字典表业务层
 */
@Service
@RequiredArgsConstructor
public class DictService {

    private final SysDictMapper sysDictMapper;

    private final SysDictItemMapper sysDictItemMapper;

    /**
     * 字典类型分页查询
     *
     * @param page
     * @param sysDict
     * @return
     * @author 张高昌
     * @date 2022/01/09 15:12
     */
    public IPage<SysDict> getDictPage(Page page, SysDict sysDict) {
        return sysDictMapper.selectPage(page, Wrappers.query(sysDict));
    }

    /**
     * 新增字典表
     *
     * @param sysDict
     * @return
     * @author 张高昌
     * @date 2022/02/07 17:16
     */
    public boolean saveDict(SysDict sysDict) {

        return sysDictMapper.insert(sysDict) > 0;
    }

    /**
     * 修改字典表
     *
     * @param sysDict
     * @return
     * @author 张高昌
     * @date 2022/02/07 17:19
     */
    @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
    public RequestResult updateDict(SysDict sysDict) {
        // 系统内置
        if (DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystem())) {
            return RequestResult.failed("系统内置字典不能修改");
        }
        sysDictMapper.updateById(sysDict);
        return RequestResult.success(true);
    }

    /**
     * 删除字典表及其关联的表
     *
     * @param id
     * @return
     * @author 张高昌
     * @date 2022/02/07 16:22
     */
    public RequestResult removeDict(int id) {
        // 查询字典
        SysDict sysDict = sysDictMapper.selectById(id);
        // 系统内置
        if (DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystem())) {
            return RequestResult.failed("系统内置字典不能删除");
        }
        sysDictMapper.deleteById(id);
        sysDictItemMapper.delete(Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getDictId, id));

        return RequestResult.success(true);
    }

    /**
     * 字典项分页查询
     *
     * @param page
     * @param sysDictItem
     * @return
     * @author 张高昌
     * @date 2022/01/09 19:42
     */
    public IPage<SysDictItem> getDictItemPage(Page page, SysDictItem sysDictItem) {

        return sysDictItemMapper.selectPage(page, Wrappers.query(sysDictItem));
    }

    /**
     * 新增字典项并且同步缓存
     *
     * @param item
     * @return
     * @author 张高昌
     * @date 2022/02/07 09:27
     */
    public RequestResult addDictItem(SysDictItem item) {
        List<SysDictItem> list = sysDictItemMapper.selectList(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getType, item.getType()).eq(SysDictItem::getLabel, item.getLabel()));
        if (list.size() > 0) {
            return RequestResult.failed("类型已存在标签名");
        }

        return RequestResult.success(sysDictItemMapper.insert(item) > 0);
    }

    /**
     * 修改字典项
     *
     * @param item
     * @return
     * @author 张高昌
     * @date 2022/02/07 09:27
     */
    public RequestResult updateDictItem(SysDictItem item) {
        // 查询字典
        SysDict sysDict = sysDictMapper.selectById(item.getDictId());
        // 系统内置
        if (DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystem())) {
            return RequestResult.failed("系统内置字典项目不能修改");
        }
        return RequestResult.success(sysDictItemMapper.updateById(item) > 0);
    }

    /**
     * 根据id删除字典项
     *
     * @param id
     * @return
     * @author 张高昌
     * @date 2022/02/07 09:27
     */
    public boolean deleteDictItem(int id) {

        return sysDictItemMapper.deleteById(id) > 0;
    }

}
